﻿using System;
using System.IO;
using System.Xml;

using Yawf.Core.Utils;

namespace Yawf.Core.Xml {
	public class XmlUtils {
		/// <summary>
		/// Reads an XMNL Documemnt from a Stream
		/// </summary>
		/// <param name="stream">Setram to read</param>
		/// <returns>XmlDocument object</returns>
		public static XmlDocument ReadXmlDocument(Stream stream) {
			XmlDocument xmlDoc;

			xmlDoc = new XmlDocument();
			xmlDoc.PreserveWhitespace = true;
			xmlDoc.Load(new StreamReader(stream));

			return xmlDoc;
		}							// ReadXmlDocument
	
		/// <summary>
		/// Reads an Xml file from disk
		/// </summary>
		/// <param name="file">FileInfoObject to use</param>
		/// <returns></returns>
		public static XmlDocument ReadXmlDocument(FileInfo file) {
			XmlDocument xmlDoc;

			xmlDoc = new XmlDocument();
			xmlDoc.PreserveWhitespace = true;
			xmlDoc.Load(file.FullName);

			return xmlDoc;
		}

		/// <summary>
		/// Reads an Xml file from disk
		/// </summary>
		/// <param name="filePath">Spring Location path, could be file://, config:// etc</param>
		/// <returns></returns>
		public static XmlDocument ReadXmlDocument(String filePath) {
			return ReadXmlDocument(ResourceUtils.GetFileResource(filePath));
		}				// ReadXmlDocument

		/// <summary>
		/// Imports a XmlElement at the end of the XmlDocument given
		/// </summary>
		/// <param name="xmlDoc">Documento to import into</param>
		/// <param name="theChild">Element to import</param>
		public static void ImportChild(XmlDocument xmlDoc,XmlElement theChild) {
			XmlNode tmpNode;

			tmpNode = xmlDoc.ImportNode(theChild,true);
			xmlDoc.DocumentElement.AppendChild(tmpNode);
		}							// ImportChild

		/// <summary>
		/// Imports a XmlElement into a XmlDocument at the end of a parent XmlElement
		/// </summary>
		/// <param name="xmlDoc">Documento to import into</param>
		/// <param name="parentElem">Parent XmlElement to append into</param>
		/// <param name="theChild">Element to import</param>
		public static void ImportChild(XmlDocument xmlDoc,XmlElement parentElem,XmlElement theChild) {
			XmlNode tmpNode;

			tmpNode = xmlDoc.ImportNode(theChild,true);
			parentElem.AppendChild(tmpNode);
		}							// ImportChild

	}
}
